package com.crazy.bookstore.service.impl;

import com.crazy.bookstore.entity.Evaluation;
import com.crazy.bookstore.mapper.EvaluationMapper;
import com.crazy.bookstore.service.EvaluationService;
import com.crazy.bookstore.vo.EvaluationVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
//默认不开启事务
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class EvaluationServiceImpl implements EvaluationService {
    @Resource
    private EvaluationMapper evaluationMapper;

    /**
     * 按书本id查询对应的短评列表
     * @param bookId
     * @return 带有短评者名字的封装类
     */
    @Override
    public List<EvaluationVO> selectByBookId(Long bookId) {
        List<EvaluationVO> list = evaluationMapper.selectByBookId(bookId);
        return list;
    }

    /**
     * 插入评论
     * @param memberId
     * @param bookId
     * @param score
     * @param content
     * @return
     */
    @Override
    public Evaluation evaluate(Long memberId, Long bookId, Integer score, String content) {
        Evaluation evaluation = new Evaluation();
        evaluation.setMemberId(memberId);
        evaluation.setBookId(bookId);
        evaluation.setScore(score);
        evaluation.setContent(content);
        evaluation.setCreateTime(new Date());
        evaluation.setState("enable");
        evaluation.setEnjoy(0);

        //插入数据库
        evaluationMapper.insert(evaluation);
        return evaluation;
    }

    /**
     * 短评点赞
     * @param evaluationId
     * @return
     */
    @Override
    public Evaluation enjoy(Long evaluationId) {
        Evaluation evaluation = evaluationMapper.selectById(evaluationId);
        //点赞加一
        evaluation.setEnjoy(evaluation.getEnjoy()+1);
        evaluationMapper.updateById(evaluation);
        return evaluation;
    }
}
